package manager;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utils.DBConnector;

/**
 * Servlet implementation class DeleteStyleSheet
 */
public class DeleteStyleSheet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteStyleSheet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
    		String username = request.getSession(false).getAttribute("username").toString();
    		if (request.getSession(false) == null || request.getSession(false).getAttribute("username") == null){
    			response.sendError(HttpServletResponse.SC_FORBIDDEN, "Please login first");
    			return;
    		}

        	DBConnector dbConn = new DBConnector();
			PreparedStatement stmnt = dbConn.getConnection().prepareStatement(
					"DELETE FROM user_stylesheets " +
					"WHERE username=?" +
					"  and style_name=?; ");
		
			stmnt.setString(1, username);
			
			String selection = request.getParameter("selection");
			stmnt.setString(2, selection);
		
            if (selection.equals("Default") || selection.equals("Custom")){
    			response.sendError(HttpServletResponse.SC_FORBIDDEN, "Can not delete Default or Custom. " +
    					"These Stylesheets are System's stylesheets.");
            	return;
            }

			
			stmnt.execute();
        
			response.sendRedirect("custom_stylesheet.jsp");
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
